package com.niit.mall.pay.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.mall.pay.domain.dto.PayOrderDTO;
import com.niit.mall.pay.domain.enums.PayStatus;
import com.niit.mall.pay.domain.po.PayOrder;
import com.niit.mall.pay.mapper.PayOrderMapper;
import com.niit.mall.pay.service.IPayOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.niit.mall.common.api.ResultCode;
import com.niit.mall.common.exception.Asserts;

/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.pay.service.impl
 * @Author: ChuXuan
 * @CreateTime: 2025-06-08  01:21
 * @Description: 支付服务实现类
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {
	
	
//	private final UserClient userClient;
	
	private final RabbitTemplate rabbitTemplate;
	
	@Override
	@Transactional
	public void tryPayOrderByBalance(PayOrderDTO payOrderDTO) {
		// 1.查询支付单
		PayOrder po = getById(payOrderDTO.getId());
		// 2.判断状态
		if(!PayStatus.WAIT_BUYER_PAY.equalsValue(po.getStatus())){
			// 订单不是未支付，状态异常
			Asserts.fail(ResultCode.FAILED);
		}
		// 3.尝试扣减余额
//		userClient.deductMoney(payOrderDTO.getPw(), po.getAmount());
		// 4.修改支付单状态
//		boolean success = markPayOrderSuccess(payOrderDTO.getId(), LocalDateTime.now());
//		if (!success) {
//			throw new BizIllegalException("交易已支付或关闭！");
//		}
		// 5.修改订单状态
		// tradeClient.markOrderPaySuccess(po.getBizOrderNo());
//		try {
//			rabbitTemplate.convertAndSend("pay.direct", "pay.success", po.getBizOrderNo());
//		} catch (Exception e) {
//			log.error("支付成功的消息发送失败，支付单id：{}， 交易单id：{}", po.getId(), po.getBizOrderNo(), e);
//		}
	}
}
